package com.kcth.sylyg08.rest;


import com.kcht.adp.common.config.ConfigurationValue;
import com.kcht.adp.common.log.Logger;
import com.kcht.sylyg08.dao.IAntDao;
import com.kcht.sylyg08.dao.IReaderDao;
import com.kcth.sylyg08.entity.Ant;
import com.kcth.sylyg08.entity.Reader;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * 自检程序
 * */
@Path("/inspect/")
@RequestScoped
@Transactional(rollbackOn=Exception.class)
public class SelfInspection {
    @Inject
    private Logger logger;
    @Inject
    @ConfigurationValue
    private String uploadLostDetailsUri;
    @Inject
    @ConfigurationValue
    private String clearAlarmUri;
    @Inject
    private IAntDao antDao;
    @Inject
    private IReaderDao readerDao;
    /**
     *
     * 由RD30driver直接上报失联的设备，
     * 然后此接口再开始上传到上层系统。
     * */
    @POST
    @Path("post")
    public void uploadLostDetails(String ip) {
        Reader reader = readerDao.getReaderByIP(ip);
        String connector=getAntConnector(ip);
        if(reader!=null){
            LostDetails lostDetails=new LostDetails();
            lostDetails.setIp(ip);
            lostDetails.setLocation(reader.getLocation());
            lostDetails.setConnector(connector);
            Response response = null;
            Client client = ClientBuilder.newClient();

            try {
                response = client.target(uploadLostDetailsUri).request().post(Entity.entity(lostDetails, "application/json"));
                logger.info("返回状态-->"+response.getStatus()+",报警位置:"+reader.getLocation());
            } catch (Exception e) {
                logger.error("连接中断，发送失联信息失败"+",报警位置:"+reader.getLocation());
            }finally {
                if(response!=null){
                    response.close();
                }
                client.close();
            }
        }else{
            logger.error("ip地址为"+ip+"阅读器未加入系统");
        }

    }


    /**
     *
     * 由RD30driver取消失联的设备，
     * 然后此接口再开始上传到上层系统。
     * */
    @DELETE
    @Path("delete/{ip}")
    public void clearAlarm(@PathParam("ip") String ip) {
        Reader reader = readerDao.getReaderByIP(ip);
        String connector=getAntConnector(ip);
        if(reader!=null){
            LostDetails lostDetails=new LostDetails();
            lostDetails.setIp(ip);
            lostDetails.setLocation(reader.getLocation());
            lostDetails.setConnector(connector);
            Response response = null;
            Client client = ClientBuilder.newClient();
            try {
                response = client.target(clearAlarmUri).request().post(Entity.entity(lostDetails, "application/json"));
                logger.info("返回状态-->"+response.getStatus()+",取消报警位置:"+reader.getLocation());
            } catch (Exception e) {
                logger.error("连接中断，发送失联信息失败"+",取消报警位置:"+reader.getLocation());
            }finally {
                if(response!=null){
                    response.close();
                }
                client.close();
            }
        }else{
            logger.error("ip地址为"+ip+"阅读器未加入系统");
        }

    }

    private String  getAntConnector(String ip){
        List<Ant> ants = antDao.selectListByJPQL("select a from Ant a where a.ip='" + ip + "'",Ant.class);
        StringBuilder connector= new StringBuilder();
        if(ants!=null&&!ants.isEmpty()){
            for (int i = 0; i < ants.size(); i++) {
                connector.append(ants.get(i).getConnect());
                if(i<(ants.size()-1)){
                    connector.append(",");
                }
            }
        }
        return connector.toString();
    }

}
